<template>
  <el-dialog
    :title="!dataForm.pmsProductId ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="商品名称" prop="pmsProductName">
      <el-input v-model="dataForm.pmsProductName" placeholder="商品名称"></el-input>
    </el-form-item>
    <el-form-item label="商品标题" prop="pmsProductTitle">
      <el-input v-model="dataForm.pmsProductTitle" placeholder="商品标题"></el-input>
    </el-form-item>
    <el-form-item label="商品图片" prop="pmsProductImg">
      <el-input v-model="dataForm.pmsProductImg" placeholder="商品图片"></el-input>
    </el-form-item>
    <el-form-item label="商品介绍" prop="pmsProductDetail">
      <el-input v-model="dataForm.pmsProductDetail" placeholder="商品介绍"></el-input>
    </el-form-item>
    <el-form-item label="商品单价" prop="pmsProductPrice">
      <el-input v-model="dataForm.pmsProductPrice" placeholder="商品单价"></el-input>
    </el-form-item>
    <el-form-item label="商品库存(-1为无限制)" prop="pmsProductStock">
      <el-input v-model="dataForm.pmsProductStock" placeholder="商品库存(-1为无限制)"></el-input>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        dataForm: {
          pmsProductId: 0,
          pmsProductName: '',
          pmsProductTitle: '',
          pmsProductImg: '',
          pmsProductDetail: '',
          pmsProductPrice: '',
          pmsProductStock: ''
        },
        dataRule: {
          pmsProductName: [
            { required: true, message: '商品名称不能为空', trigger: 'blur' }
          ],
          pmsProductTitle: [
            { required: true, message: '商品标题不能为空', trigger: 'blur' }
          ],
          pmsProductImg: [
            { required: true, message: '商品图片不能为空', trigger: 'blur' }
          ],
          pmsProductDetail: [
            { required: true, message: '商品介绍不能为空', trigger: 'blur' }
          ],
          pmsProductPrice: [
            { required: true, message: '商品单价不能为空', trigger: 'blur' }
          ],
          pmsProductStock: [
            { required: true, message: '商品库存(-1为无限制)不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.pmsProductId = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.pmsProductId) {
            this.$http({
              url: this.$http.adornUrl(`/product/product/info/${this.dataForm.pmsProductId}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.pmsProductName = data.product.pmsProductName
                this.dataForm.pmsProductTitle = data.product.pmsProductTitle
                this.dataForm.pmsProductImg = data.product.pmsProductImg
                this.dataForm.pmsProductDetail = data.product.pmsProductDetail
                this.dataForm.pmsProductPrice = data.product.pmsProductPrice
                this.dataForm.pmsProductStock = data.product.pmsProductStock
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/product/product/${!this.dataForm.pmsProductId ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'pmsProductId': this.dataForm.pmsProductId || undefined,
                'pmsProductName': this.dataForm.pmsProductName,
                'pmsProductTitle': this.dataForm.pmsProductTitle,
                'pmsProductImg': this.dataForm.pmsProductImg,
                'pmsProductDetail': this.dataForm.pmsProductDetail,
                'pmsProductPrice': this.dataForm.pmsProductPrice,
                'pmsProductStock': this.dataForm.pmsProductStock
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
